package com.slz3.business.module.drawManagement.draFreeLottery.domain;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.LocalDT;
import com.slz3.business.frame.util.UUIDUtils;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.drawManagement.draFreeLottery.enums.DraFreeLotteryGoodsDrawPeopleFlag;
import org.springframework.util.MultiValueMap;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

public class DraFreeLotteryFactory {

    /**
     * 初始化基础属性值
     */
    public static DraFreeLottery initBaseFields(DraFreeLottery draFreeLottery) {
        if (draFreeLottery.getUuid() == null) {
            draFreeLottery.setUuid(UUIDUtils.generateUUID());
            draFreeLottery.setCreateTime(LocalDateTime.now());
            if (VE.isEmpty(draFreeLottery.getSerAudit())) {
                draFreeLottery.setSerAudit(AuditFlag.WAIT_SUBMIT);
            }
            if (VE.isEmpty(draFreeLottery.getSerUsing())) {
                draFreeLottery.setSerUsing(UsingFlag.ENABLE);
            }
        } else {
            draFreeLottery.setLastModifyTime(LocalDateTime.now());
        }
        return draFreeLottery;
    }

    /**
     * 初始化基础属性值
     */
    public static DraFreeLottery initBaseFields() {
        return initBaseFields(newTemStyle());
    }

    public static DraFreeLottery newTemStyle() {
        return new DraFreeLottery();
    }

    public static DraFreeLotteryCondition newDraFreeLotteryCondition(MultiValueMap<String, String> params) {

        DraFreeLotteryCondition condition = new DraFreeLotteryCondition();

        String createTimeOUTSET = params.getFirst("createTimeOUTSET");
        String createTimeFINISH = params.getFirst("createTimeFINISH");
        String lastModifyTimeOUTSET = params.getFirst("lastModifyTimeOUTSET");
        String lastModifyTimeFINISH = params.getFirst("lastModifyTimeFINISH");
        String serAuditList = params.getFirst("serAuditList");

        condition.setCreateTimeOUTSET(VE.isEmpty(createTimeOUTSET) ? null : LocalDT.parseStringToDateTime(createTimeOUTSET, DraFreeLotteryCondition.CREATE_TIME_OF_PATTERN));
        condition.setCreateTimeFINISH(VE.isEmpty(createTimeFINISH) ? null : LocalDT.parseStringToDateTime(createTimeFINISH, DraFreeLotteryCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeOUTSET(VE.isEmpty(lastModifyTimeOUTSET) ? null : LocalDT.parseStringToDateTime(lastModifyTimeOUTSET, DraFreeLotteryCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeFINISH(VE.isEmpty(lastModifyTimeFINISH) ? null : LocalDT.parseStringToDateTime(lastModifyTimeFINISH, DraFreeLotteryCondition.CREATE_TIME_OF_PATTERN));
        condition.setSerAuditList(VE.isEmpty(serAuditList) ? null : Arrays.stream(serAuditList.split(",")).map(item -> AuditFlag.valueOf(item)).collect(Collectors.toList()));

        return condition;

    }

    /**
     * 向redis存入商品抽奖号码
     * @param draFreeLottery
     */
    public static void saveDraFreeLotteryNumber(DraFreeLottery draFreeLottery) {
        DraFreeLotteryGoodsDrawPeopleFlag serGoodsDrawPeople = draFreeLottery.getSerGoodsDrawPeople();

        List list = new ArrayList();

        UUID uuid = draFreeLottery.getUuid();

        if (DraFreeLotteryGoodsDrawPeopleFlag.SHIRENTUAN.equals(serGoodsDrawPeople)) {
            for (int i = 0; i < 10; i++) {
                list.add("1000" + i);
            }
            String ten = Steward.toJson(list);
            Jedis.db0.set(String.format("%s:%s:%s:%s", RK.CONFIG_DOMIN, RK.DRA_FREE_LOTTERY,RK.NUMBER, uuid), ten);
        }
        if (DraFreeLotteryGoodsDrawPeopleFlag.BAIRENTUAN.equals(serGoodsDrawPeople)) {
            for (int i = 0; i < 100; i++) {
                if (i < 10) {
                    list.add("1000" + i);

                } else {
                    list.add("100" + i);
                }
            }
            String bai = Steward.toJson(list);
            Jedis.db0.set(String.format("%s:%s:%s:%s", RK.CONFIG_DOMIN, RK.DRA_FREE_LOTTERY, RK.NUMBER, uuid), bai);
        }
        if (DraFreeLotteryGoodsDrawPeopleFlag.QIANRENTUAN.equals(serGoodsDrawPeople)) {
            for (int i = 0; i < 1000; i++) {

                if (i < 10) {
                    list.add("1000" + i);

                } else if (i < 100 && i > 9) {
                    list.add("100" + i);
                } else {
                    list.add("10" + i);
                }
            }
            String qian = Steward.toJson(list);
            Jedis.db0.set(String.format("%s:%s:%s:%s", RK.CONFIG_DOMIN, RK.DRA_FREE_LOTTERY,RK.NUMBER, uuid), qian);
        }
    }
}
